package com.ohy.tencent.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ohy.tencent.util.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

@RestController
@RequestMapping("douyin")
@Slf4j
public class DouyinController {
    @PostMapping(value = "getDownloadUrl")
    public String getSearchRes(@RequestParam(value = "word") String url) {
        try {
            url = url.contains("复制此链接") ? url.substring(url.indexOf("https://v.douyin.com"), url.indexOf("复制此链接，打开【抖音短视频】")) : url;
            url = url.trim();
            HttpURLConnection conn = (HttpURLConnection) new URL(url)
                    .openConnection();
            conn.setInstanceFollowRedirects(false);
            conn.setConnectTimeout(5000);
            url = conn.getHeaderField("Location");
            String itemId = url.substring(url.indexOf("video/") + "video/".length(), url.length());
            itemId = itemId.substring(0, itemId.indexOf("/"));
            url = "https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=" + itemId;
            HttpGet httpGet = new HttpGet(url);
            CloseableHttpClient client = HttpClients.createDefault();
            CloseableHttpResponse response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            String result = EntityUtils.toString(entity);
            JSONObject jsonObject = JSONObject.parseObject(result);
            String vid = jsonObject.getJSONArray("item_list").getJSONObject(0).getJSONObject("video").getString("vid");
            String desc = jsonObject.getJSONArray("item_list").getJSONObject(0).getString("desc");
            url = "https://api.amemv.com/aweme/v1/play/?video_id=" + vid + "&line=1&ratio=540p&media_type=4&vr_type=0&improve_bitrate=0&is_play_url=1&is_support_h265=0&source=PackSourceEnum_PUBLISH";
            // String url2 = "https://aweme.snssdk.com/aweme/v1/play/?video_id=" + vid + "&line=0&ratio=540p&media_type=4&vr_type=0&improve_bitrate=0&is_play_url=1&is_support_h265=0&source=PackSourceEnum_PUBLISH";
            log.info("文案：" + desc);
            conn = (HttpURLConnection) new URL(url)
                    .openConnection();
            conn.setInstanceFollowRedirects(false);
            conn.setRequestProperty("user-agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1");
            conn.setConnectTimeout(5000);
            url = conn.getHeaderField("Location");
            log.info("下载地址为：" + url);
            return url;
        } catch (Exception e) {
            log.info("获取下载地址异常");
            return "获取下载地址异常";
        }
    }

    @GetMapping("douyin_hot")
    public AjaxResult douyin_hot() throws IOException {
        String url = "https://www.iesdouyin.com/web/api/v2/hotsearch/billboard/aweme/";
        JSONArray jsonArray = new JSONArray(20);
        String s = HttpUtil.get(url);
        JSONObject jsonObject = JSONObject.parseObject(s);
        JSONArray array = jsonObject.getJSONArray("aweme_list");
        HttpURLConnection conn;
        for (int i = 0; i < array.size(); i++) {
            String desc = array.getJSONObject(i).getJSONObject("aweme_info").getString("desc");
            String vid = array.getJSONObject(i).getJSONObject("aweme_info").getJSONObject("video").getString("vid");
            String img = array.getJSONObject(i).getJSONObject("aweme_info").getJSONObject("video").getJSONObject("origin_cover").getJSONArray("url_list").getString(0);
            String uid = array.getJSONObject(i).getJSONObject("aweme_info").getJSONObject("author").getString("uid");
            url = "https://api.amemv.com/aweme/v1/play/?video_id=" + vid + "&line=1&ratio=540p&media_type=4&vr_type=0&improve_bitrate=0&is_play_url=1&is_support_h265=0&source=PackSourceEnum_PUBLISH";
            conn = (HttpURLConnection) new URL(url)
                    .openConnection();
            conn.setInstanceFollowRedirects(false);
            conn.setConnectTimeout(5000);
            // String url2 = "https://aweme.snssdk.com/aweme/v1/play/?video_id=" + vid + "&line=0&ratio=540p&media_type=4&vr_type=0&improve_bitrate=0&is_play_url=1&is_support_h265=0&source=PackSourceEnum_PUBLISH";
            conn.setRequestProperty("user-agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1");
            url = conn.getHeaderField("Location");

            JSONObject data = new JSONObject();
            data.put("desc", desc);
            data.put("img", img);
            data.put("uid", uid);
            data.put("url", url);
            jsonArray.add(data);
        }
        return AjaxResult.success(jsonArray);
    }
}


